import { call, put, takeEvery } from "redux-saga/effects";
//模拟接口
const Userservice = {
    login(name) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                if (name === "kaikeba") {
                    resolve({ name: "kaikeba" })
                } else {
                    reject("用户名或密码错误,请在手机号输入框输入kaikeba")
                }
            }, 1500)
        })
    }
}


//worker
function* loginHandle(action) {
    try {
        yield put({ type: "logining" });
        const res = yield call(Userservice.login, action.name);
        yield put({ type: "loginSuccess", res })
    } catch (err) {
        yield put({ type: "loginError", err });
    }
}

//watcher saga
function* mySaga() {
    yield takeEvery("login", loginHandle) //login为任务名
}

export default mySaga;